﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectCloth.Entity;
using ProjectCloth.FrmUI.Common;

namespace ProjectCloth.FrmUI
{
    public partial class GeneralExpensesStatistics : UserControl
    {
        private ClothContext db = new ClothContext();
        public GeneralExpensesStatistics()
        {
            InitializeComponent();
        }
        
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dataGridView1.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dataGridView1.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            var subjectList = db.Subjects.ToList();
            var linqstr = from gi in db.GeneralExpensesItems.ToList() join ge in db.GeneralExpenseses.ToList() on gi.GeneralExpensesId equals ge.Id where(ge.CreateTime>startDatePiker.Value&&ge.CreateTime<endDatePicker.Value)
                          group gi by gi.SubjectName into s
                          select new
                          {
                              SubjectName = s.Key,
                              Amount = s.Sum(o => o.Cost)
                          };
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = linqstr.ToList();
        }

        private void exportBtn_Click(object sender, EventArgs e)
        {
            Helper.ExportDgv(dataGridView1,"一般费用统计");
        }
    }
}
